package com.qingyun.system.controller;

import com.qingyun.system.common.result.Result;
import com.qingyun.system.model.dto.FileInfo;
import com.qingyun.system.model.dto.TreeFileList;
import com.qingyun.system.service.OssService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件接口
 *
 * @author LY
 */
@Tag(name = "07.文件接口")
@RestController
@RequestMapping("/api/v1/files")
@RequiredArgsConstructor
public class FileController {

    private final OssService ossService;

    @PostMapping
    @Operation(summary = "文件上传")
    public Result<FileInfo> uploadFile(
            @Parameter(description = "表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        FileInfo fileInfo = ossService.uploadFile(file);
        return Result.success(fileInfo);
    }

    @PostMapping("/avatar")
    @Operation(summary = "头像上传")
    public Result<FileInfo> uploadAvatar(
            @Parameter(description = "表单文件对象") @RequestParam(value = "file") MultipartFile file
    ) {
        FileInfo fileInfo = ossService.uploadAvatar(file);
        return Result.success(fileInfo);
    }

    @GetMapping("/list")
    @Operation(summary = "获取文件列表")
    public Result<List<TreeFileList>> getFileList() throws Exception {
        List<TreeFileList> fileList = ossService.getFileList();
        return Result.success(fileList);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件")
    @PreAuthorize("@ss.hasPerm('sys:file:delete')")
    public Result deleteFile(@RequestParam("fileName") String fileName) throws Exception {
        ossService.deleteFile(fileName);
        return Result.success();
    }

    @DeleteMapping("/deleteFiles")
    @Operation(summary = "批量删除文件")
    @PreAuthorize("@ss.hasPerm('sys:file:delete')")
    public Result deleteFiles(@RequestBody List<String> fileNames) throws Exception {
        // 批量删除文件
        ossService.deleteFileBatch(fileNames);
        return Result.success();
    }

    @GetMapping("/downLoadFile")
    @Operation(summary = "文件下载")
    @PreAuthorize("@ss.hasPerm('sys:file:download')")
    public ResponseEntity<InputStreamResource> fileDownLoad(@RequestParam("fileName") String fileName) {
        return ossService.downLoadFile(fileName);
    }

    @PostMapping("/uploadFiles")
    @Operation(summary = "批量文件上传")
    public Result<List<FileInfo>> uploadFiles(@RequestBody List<MultipartFile> files) {
        System.out.println(files);
        List<FileInfo> fileInfos = ossService.uploadFiles(files);
        return Result.success(fileInfos);
    }

    @PostMapping("/downLoadZip")
    @Operation(summary = "文件打包下载")
    public ResponseEntity<byte[]> downLoadZip(@RequestBody List<String> fileNames) {
        return ossService.downLoadFileZip(fileNames);
    }
}